package com.shuyun.ccms.encrypt.common;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shuyun.ccms.encrypt.util.ConnectionUtil;
import com.shuyun.crypt.CryptTools;

public class EncryptOrderIncrementData {
	private static Logger logger = LoggerFactory.getLogger(EncryptOrderIncrementData.class);
	private static final Integer COMMIT_NUMBER = 10000;

	public static void encrytYhdData(Connection conn)throws Exception{
		PreparedStatement ps = null;
		Statement insertSt = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		StringBuffer infoSql = null;
		logger.info("增量加密一号店订单信息开始");
		long start = System.currentTimeMillis();
		int count = 1;
		try {
			sql.append("select tid,customerno,receiver_name,receiver_mobile  ");
			sql.append("from plt_yhd_order pdc  ");
			sql.append("where not exists (  ");
			sql.append("select order_id  ");
			sql.append("from tb_yhd_order_info tc  ");
			sql.append("where  pdc.tid  = tc.order_id)   ");
			ps = conn.prepareStatement(sql.toString());
			rs = ps.executeQuery();
			insertSt = conn.createStatement();
			conn.setAutoCommit(false);
			while(rs.next()){
				infoSql = new StringBuffer();

				infoSql.append("insert ignore into tb_yhd_order_info(order_id, customerno_encrypt,receiver_name_encrypt, receiver_mobile_encrypt)");
				infoSql.append("values('");

				String tid = rs.getString("tid");
				String customerNo = rs.getString("customerno");
				String receiveName = rs.getString("receiver_name");
				String receiveMobile = rs.getString("receiver_mobile");

				String encryptCustomerNo = null;
				String encryptReceiveName = null;
				String encryptReceiveMobile = null;

				infoSql.append(tid).append("',");
				if(customerNo != null){
					encryptCustomerNo = CryptTools.encrypt(customerNo, CryptTools.TYPE_NICK);
					infoSql.append("'").append(encryptCustomerNo).append("',");
				}else{
					infoSql.append(encryptCustomerNo).append(",");
				}
				if(receiveName != null){
					encryptReceiveName = CryptTools.encrypt(receiveName, CryptTools.TYPE_RECEIVER_NAME);
					infoSql.append("'").append(encryptReceiveName).append("',");
				}else{
					infoSql.append(encryptReceiveName).append(",");
				}

				if(StringUtils.isNotBlank(receiveMobile)){
					encryptReceiveMobile = CryptTools.encrypt(receiveMobile, CryptTools.TYPE_PHONE);
					infoSql.append("'").append(encryptReceiveMobile).append("')");
				}else{
					infoSql.append(encryptReceiveMobile).append(")");
				}

				System.out.println("yhd infosql: " + infoSql.toString());
				insertSt.addBatch(infoSql.toString());
				if(count % COMMIT_NUMBER == 0){
					insertSt.executeBatch();
					conn.commit();
					insertSt.clearBatch();
					logger.info("一号店提交订单信息数据. count: " + count);
					count=1;
				}
				count++;
			}
			insertSt.executeBatch();
			conn.commit();
			insertSt.clearBatch();
			long end = System.currentTimeMillis();
			logger.info("增量加密一号店订单信息结束,耗时: " + (end -start)/1000 + "s");
		} catch (Exception e) {
			conn.rollback();
			e.printStackTrace();
			logger.error("增量加密一号店订单信息数据失败. " + e.getMessage());
			throw e;
		}finally{
			ConnectionUtil.newInstance().close(ps, rs);
			ConnectionUtil.newInstance().closeStatement(insertSt);
		}
	}

	public static void encrytJdData(Connection conn)throws Exception{
		PreparedStatement ps = null;
		Statement insertSt = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		StringBuffer infoSql = null;
		logger.info("增量加密京东订单信息开始");
		long start = System.currentTimeMillis();
		int count = 1;
		try {
			sql.append("select order_id,pin,fullname,mobile  ");
			sql.append("from plt_jd_order pdc  ");
			sql.append("where not exists (  ");
			sql.append("select order_id  ");
			sql.append("from tb_jd_order_info tc  ");
			sql.append("where  pdc.order_id  = tc.order_id)   ");
			ps = conn.prepareStatement(sql.toString());
			rs = ps.executeQuery();
			insertSt = conn.createStatement();
			conn.setAutoCommit(false);
			while(rs.next()){
				infoSql = new StringBuffer();

				infoSql.append("insert ignore into tb_jd_order_info(order_id, customerno_encrypt,receiver_name_encrypt, receiver_mobile_encrypt)");
				infoSql.append("values('");

				String tid = rs.getString("order_id");
				String customerNo = rs.getString("pin");
				String receiveName = rs.getString("fullname");
				String receiveMobile = rs.getString("mobile");

				String encryptCustomerNo = null;
				String encryptReceiveName = null;
				String encryptReceiveMobile = null;

				infoSql.append(tid).append("',");
				if(customerNo != null){
					encryptCustomerNo = CryptTools.encrypt(customerNo, CryptTools.TYPE_NICK);
					infoSql.append("'").append(encryptCustomerNo).append("',");
				}else{
					infoSql.append(encryptCustomerNo).append(",");
				}
				if(receiveName != null){
					encryptReceiveName = CryptTools.encrypt(receiveName, CryptTools.TYPE_RECEIVER_NAME);
					infoSql.append("'").append(encryptReceiveName).append("',");
				}else{
					infoSql.append(encryptReceiveName).append(",");
				}

				if(StringUtils.isNotBlank(receiveMobile)){
					encryptReceiveMobile = CryptTools.encrypt(receiveMobile, CryptTools.TYPE_PHONE);
					infoSql.append("'").append(encryptReceiveMobile).append("')");
				}else{
					infoSql.append(encryptReceiveMobile).append(")");
				}

				System.out.println("jd infosql: " + infoSql.toString());
				insertSt.addBatch(infoSql.toString());
				if(count % COMMIT_NUMBER == 0){
					insertSt.executeBatch();
					conn.commit();
					insertSt.clearBatch();
					logger.info("京东提交订单信息数据. count: " + count);
					count=1;
				}
				count++;
			}
			insertSt.executeBatch();
			conn.commit();
			insertSt.clearBatch();
			long end = System.currentTimeMillis();
			logger.info("增量加密京东订单信息结束,耗时: " + (end -start)/1000 + "s");
		} catch (Exception e) {
			conn.rollback();
			e.printStackTrace();
			logger.error("增量加密京东订单信息数据失败. " + e.getMessage());
			throw e;
		}finally{
			ConnectionUtil.newInstance().close(ps, rs);
			ConnectionUtil.newInstance().closeStatement(insertSt);
		}
	}

	public static void encrytDdData(Connection conn)throws Exception{
		PreparedStatement ps = null;
		Statement insertSt = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		StringBuffer infoSql = null;
		logger.info("增量加密当当订单信息开始");
		long start = System.currentTimeMillis();
		int count = 1;
		try {
			sql.append("select order_id,customerno,consignee_name,consignee_mobile_tel ");
			sql.append("from plt_dd_order pdc  ");
			sql.append("where not exists (  ");
			sql.append("select order_id  ");
			sql.append("from tb_dd_order_info tc  ");
			sql.append("where  pdc.order_id  = tc.order_id)   ");
			ps = conn.prepareStatement(sql.toString());
			rs = ps.executeQuery();
			insertSt = conn.createStatement();
			conn.setAutoCommit(false);
			while(rs.next()){
				infoSql = new StringBuffer();

				infoSql.append("insert ignore into tb_dd_order_info(order_id, customerno_encrypt,receiver_name_encrypt, receiver_mobile_encrypt)");
				infoSql.append("values('");

				String tid = rs.getString("order_id");
				String customerNo = rs.getString("customerno");
				String receiveName = rs.getString("consignee_name");
				String receiveMobile = rs.getString("consignee_mobile_tel");

				String encryptCustomerNo = null;
				String encryptReceiveName = null;
				String encryptReceiveMobile = null;

				infoSql.append(tid).append("',");
				if(customerNo != null){
					encryptCustomerNo = CryptTools.encrypt(customerNo, CryptTools.TYPE_NICK);
					infoSql.append("'").append(encryptCustomerNo).append("',");
				}else{
					infoSql.append(encryptCustomerNo).append(",");
				}
				if(receiveName != null){
					encryptReceiveName = CryptTools.encrypt(receiveName, CryptTools.TYPE_RECEIVER_NAME);
					infoSql.append("'").append(encryptReceiveName).append("',");
				}else{
					infoSql.append(encryptReceiveName).append(",");
				}

				if(StringUtils.isNotBlank(receiveMobile)){
					encryptReceiveMobile = CryptTools.encrypt(receiveMobile, CryptTools.TYPE_PHONE);
					infoSql.append("'").append(encryptReceiveMobile).append("')");
				}else{
					infoSql.append(encryptReceiveMobile).append(")");
				}

				System.out.println("yhd infosql: " + infoSql.toString());
				insertSt.addBatch(infoSql.toString());
				if(count % COMMIT_NUMBER == 0){
					insertSt.executeBatch();
					conn.commit();
					insertSt.clearBatch();
					logger.info("当当提交订单信息数据. count: " + count);
					count=1;
				}
				count++;
			}
			insertSt.executeBatch();
			conn.commit();
			insertSt.clearBatch();
			long end = System.currentTimeMillis();
			logger.info("增量加密当当订单信息结束,耗时: " + (end -start)/1000 + "s");
		} catch (Exception e) {
			conn.rollback();
			e.printStackTrace();
			logger.error("增量加密当当订单信息数据失败. " + e.getMessage());
			throw e;
		}finally{
			ConnectionUtil.newInstance().close(ps, rs);
			ConnectionUtil.newInstance().closeStatement(insertSt);
		}
	}

	public static void encrytTaobaoData(Connection conn)throws Exception{
		PreparedStatement ps = null;
		Statement insertSt = null;
		ResultSet rs = null;
		StringBuffer sql = new StringBuffer();
		StringBuffer infoSql = null;
		logger.info("增量加密淘宝订单信息开始");
		long start = System.currentTimeMillis();
		int count = 1;
		try {
			sql.append("select tid,customerno,receiver_name,receiver_mobile ");
			sql.append("from plt_taobao_order pdc  ");
			sql.append("where not exists (  ");
			sql.append("select order_id  ");
			sql.append("from tb_taobao_order_info tc  ");
			sql.append("where  pdc.tid  = tc.order_id)   ");
			ps = conn.prepareStatement(sql.toString());
			rs = ps.executeQuery();
			insertSt = conn.createStatement();
			conn.setAutoCommit(false);
			while(rs.next()){
				infoSql = new StringBuffer();

				infoSql.append("insert ignore into tb_taobao_order_info(order_id, customerno_encrypt,receiver_name_encrypt, receiver_mobile_encrypt)");
				infoSql.append("values('");

				String tid = rs.getString("tid");
				String customerNo = rs.getString("customerno");
				String receiveName = rs.getString("receiver_name");
				String receiveMobile = rs.getString("receiver_mobile");

				String encryptCustomerNo = null;
				String encryptReceiveName = null;
				String encryptReceiveMobile = null;

				infoSql.append(tid).append("',");
				if(customerNo != null){
					encryptCustomerNo = CryptTools.encrypt(customerNo, CryptTools.TYPE_NICK);
					infoSql.append("'").append(encryptCustomerNo).append("',");
				}else{
					infoSql.append(encryptCustomerNo).append(",");
				}
				if(receiveName != null){
					encryptReceiveName = CryptTools.encrypt(receiveName, CryptTools.TYPE_RECEIVER_NAME);
					infoSql.append("'").append(encryptReceiveName).append("',");
				}else{
					infoSql.append(encryptReceiveName).append(",");
				}

				if(StringUtils.isNotBlank(receiveMobile)){
					encryptReceiveMobile = CryptTools.encrypt(receiveMobile, CryptTools.TYPE_PHONE);
					infoSql.append("'").append(encryptReceiveMobile).append("')");
				}else{
					infoSql.append(encryptReceiveMobile).append(")");
				}

				System.out.println("taobao infosql: " + infoSql.toString());
				insertSt.addBatch(infoSql.toString());
				if(count % COMMIT_NUMBER == 0){
					insertSt.executeBatch();
					conn.commit();
					insertSt.clearBatch();
					logger.info("淘宝提交订单信息数据. count: " + count);
					count=1;
				}
				count++;
			}
			insertSt.executeBatch();
			conn.commit();
			insertSt.clearBatch();
			long end = System.currentTimeMillis();
			logger.info("增量加密淘宝订单信息结束,耗时: " + (end -start)/1000 + "s");
		} catch (Exception e) {
			conn.rollback();
			e.printStackTrace();
			logger.error("增量加密淘宝订单信息数据失败. " + e.getMessage());
			throw e;
		}finally{
			ConnectionUtil.newInstance().close(ps, rs);
			ConnectionUtil.newInstance().closeStatement(insertSt);
		}
	}

}
